﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Newtonsoft.Json.Linq;
using System.IO;
using System.Diagnostics;
using System.Threading;

namespace MicroTest
{

    public partial class DrawingWindow : UserControl
    {
        /// <summary>
        /// 每个笔画的坐标点
        /// </summary>
        private List<Point> strokePoints;
        /// <summary>
        /// 落笔时间戳
        /// </summary>
        private DateTime penDownTime;
        /// <summary>
        /// 书写轨迹图片集合，key值为点阵地址
        /// </summary>
        private Dictionary<string, string> dicPagesPath;

        Graphics graphics = null;
        /// <summary>
        /// 当前的点阵地址
        /// </summary>
        private string curpageSerial = string.Empty;
        /// <summary>
        /// 点阵笔序列号
        /// </summary>
        private string penSerial = string.Empty;


        //private bool IsSavingData = false;
        /// <summary>
        /// 是否正在考试
        /// </summary>
        public bool IsTesting = true;

        /// <summary>
        /// 学生名
        /// </summary>
        private string studentName = string.Empty;
        /// <summary>
        /// 是否设置了本窗口的学生名
        /// </summary>
        public bool isSetname = false;

        /// <summary>
        /// 试卷背景图片
        /// </summary>
        private Dictionary<string, Image> dicPaperBackgrounds;
        private const int A4_WIDTH = 210;
        private const int A4_HEIGHT = 297;

        private const int A4_WIDTH_DRAW = 210 * 4;
        private const int A4_HEIGHT_DRAW = 297 * 4;
        /// <summary>
        /// 是否加试卷/sym背景
        /// </summary>
        private bool bShowPaperBackground = true;
        private bool ResumeMode = false;
        private Pen borderPen;
        public DrawingWindow(string penserial)
        {
            InitializeComponent();
            strokePoints = new List<Point>();
            dicPagesPath = new Dictionary<string, string>();
            dicPaperBackgrounds = new Dictionary<string, Image>();
            penSerial = penserial;
            borderPen = new Pen(Color.FromArgb(0xc7, 0xd5, 0xe0), 1);
        }
        public DrawingWindow(string penserial, Dictionary<string, Image> backImage, bool resume = false)
        {
            InitializeComponent();
            strokePoints = new List<Point>();
            dicPagesPath = new Dictionary<string, string>();
            dicPaperBackgrounds = backImage;
            penSerial = penserial;
            borderPen = new Pen(Color.FromArgb(0xc7, 0xd5, 0xe0), 1);
            labPenID.Text = penserial;
            ResumeMode = resume;
        }
        /// <summary>
        /// 获取绘画图片地址
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, string> GetDrawingImagesPath()
        {
            return dicPagesPath;
        }
        public void SaveDrawingImage()
        {
            if (!string.IsNullOrEmpty(curpageSerial))
            {
                if (picDraw.Image != null && dicPagesPath.ContainsKey(curpageSerial))
                    picDraw.Image.Save(dicPagesPath[curpageSerial]);
            }

        }
        /// <summary>
        /// 设置是否加载绘制背景图片
        /// </summary>
        /// <param name="visable"></param>
        public void SetDrawingBackgroundVisibility(bool visable)
        {
            if (visable == bShowPaperBackground)
                return;
            bShowPaperBackground = visable;
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.DrawRectangle(borderPen, 0, 0, this.Width - 1, this.Height - 1);
            base.OnPaint(e);
        }
        /// <summary>
        /// 笔连接事件
        /// </summary>
        /// <param name="penserial"></param>
        public void PenConnectedEvent(string penserial)
        {
            if (this.InvokeRequired)
            {
                Action<string> ac = new Action<string>(PenConnectedEvent);
                this.Invoke(ac, new object[] { penserial });
            }
            else
            {
                labConnStatus.ForeColor = Color.Green;
                labConnStatus.Text = "已连接";
                if (!isSetname)
                    labPenID.Text = penserial;
            }
        }
        /// <summary>
        /// 笔断开连接事件
        /// </summary>
        /// <param name="penserial"></param>
        public void PenDisconnectedEvent(string penserial)
        {
            if (this.InvokeRequired)
            {
                Action<string> ac = new Action<string>(PenDisconnectedEvent);
                this.Invoke(ac, new object[] { penserial });
            }
            else
            {
                labConnStatus.ForeColor = Color.Red;
                labConnStatus.Text = "未连接";
            }
        }
        /// <summary>
        /// 删除缓存的笔迹绘制图片目录
        /// </summary>
        private void DeleteDrawingImageFolder()
        {
            string baseFolder = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\WiseClass\\" + penSerial;
            try
            {
                if (Directory.Exists(baseFolder))
                {
                    Directory.Delete(baseFolder, true);
                    ELogger.Trace("删除缓存背景成功");
                }
            }
            catch (Exception ex)
            {

                ELogger.Error(string.Format("删除文件夹出错，{0}", ex.Message));
            }

        }
        /// <summary>
        /// 绘制笔迹图片
        /// </summary>
        /// <returns></returns>
        private string GetDrawingImageFolder()
        {
            string baseFolder = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\WiseClass";
            if (!Directory.Exists(baseFolder))
                Directory.CreateDirectory(baseFolder);
            baseFolder = baseFolder + "\\" + penSerial;
            if (!Directory.Exists(baseFolder))
                Directory.CreateDirectory(baseFolder);
            return baseFolder;

        }
        /// <summary>
        /// 书写时坐标事件
        /// </summary>
        /// <param name="penserial">笔序列号</param>
        /// <param name="pageserial">点阵地址</param>
        /// <param name="x">点阵横坐标</param>
        /// <param name="y">点阵纵坐标</param>
        public void PenCoordinates(string penserial, string pageserial, int x, int y)
        {
            if (this.InvokeRequired)
            {
                Action<string, string, int, int> ac = new Action<string, string, int, int>(PenCoordinates);
                this.Invoke(ac, new object[] { penserial, pageserial, x, y });

            }
            else
            {
                //将坐标数据加入到当前笔画的数据中
                strokePoints.Add(new Point(x, y));
                //if (Program.MTInfo.IsStudentCardPage(pageserial))
                //    return;

                //当前图片集合中不含此点阵地址对应的图片，则创建图片并加入到集合中
                //if (dicPages.ContainsKey(pageserial) == false)
                if (dicPagesPath.ContainsKey(pageserial) == false)
                {
                    try
                    {
                        ELogger.Trace(string.Format("{0}: 图片集合不包含 页码 {1}", penSerial, pageserial));
                        Bitmap img = new Bitmap(420, 297);
                        string backImageFile = GetDrawingImageFolder() + "\\" + pageserial + ".png";
                        img.Save(backImageFile);
                        img.Dispose();
                        dicPagesPath.Add(pageserial, backImageFile);
                        ELogger.Trace(string.Format("{0}: 成功将页码为 {1} 的图片添加到图片集合", penSerial, pageserial));
                    }
                    catch (Exception ex)
                    {
                        ELogger.Trace(string.Format("{0}: Fatal error!!!!!!!!!!!!!!!!!!!!!!!!!", penserial));
                        ELogger.Error(string.Format("Exception 01，笔序号:{0}", penserial));
                        ELogger.Error(string.Format("Exception 01，异常信息:{0}", ex.Message));
                        ELogger.Error(string.Format("Exception 01，栈信息:{0}", ex.StackTrace));
                    }

                }
                //更新当前点阵地址
                if (string.IsNullOrEmpty(curpageSerial))
                {
                    try
                    {
                        ELogger.Trace(string.Format("{0}: 当前图片为 null, 设置地址为 {1} 的图片", penserial, pageserial));
                        if (graphics != null)
                        {

                            picDraw.Image = null;
                            graphics.Dispose();
                            graphics = null;
                            ELogger.Trace("当前图片为 null, 释放掉 graphics");
                        }
                        Image img = Image.FromFile(dicPagesPath[pageserial]);
                        Image bitmap = new Bitmap(img);
                        img.Dispose();
                        graphics = Graphics.FromImage(bitmap);

                        if (/*dicPages[pageserial]*/bitmap.Width >/*dicPages[pageserial]*/bitmap.Height)
                        {
                            float scal = (float)(2 * A4_WIDTH * 0.8 / (2 * A4_WIDTH / 0.3 * 8));
                            graphics.ScaleTransform(scal, scal);
                        }
                        else
                        {
                            float scal = (float)(A4_WIDTH_DRAW / (A4_WIDTH / 0.3 * 8));
                            graphics.ScaleTransform(scal, scal);
                        }
                        graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                        picDraw.Image = bitmap;
                        curpageSerial = pageserial;
                        ELogger.Trace(string.Format("{0}: 当前图片为 null. 初始化 Graphics,当前图片地址为 {1}", penserial, pageserial));
                        if (bShowPaperBackground && dicPaperBackgrounds.ContainsKey(pageserial))
                        {
                            picDraw.BackgroundImage = dicPaperBackgrounds[pageserial];
                            this.picDraw.BackgroundImageLayout = ImageLayout.Zoom;
                            ELogger.Trace(string.Format("{0}: 将试卷背景图片设置为：{1}", penserial, pageserial));

                        }
                        else
                        {
                            picDraw.BackgroundImage = null;
                            ELogger.Trace(string.Format("{0}: 将试卷背景图片设置为：NULL", penserial));

                        }

                    }
                    catch (Exception ex)
                    {

                        ELogger.Trace("!!!!!!!!!!!!!!!!!!!!03");
                        ELogger.Error(string.Format("Exception 03，笔序号:{0}", penserial));
                        ELogger.Error(string.Format("Exception 03，异常信息:{0}", ex.Message));
                        ELogger.Error(string.Format("Exception 03，栈信息:{0}", ex.StackTrace));
                    }


                }
                //如果前后书写的点阵地址不一致，则将图片切换到最新的点阵地址对应的图片，并更新点阵地址
                if (curpageSerial.Equals(pageserial) == false)
                {
                    try
                    {
                        string backImageFileTmp = GetDrawingImageFolder() + "\\" + curpageSerial + "_1.png";
                        string backImageFile = GetDrawingImageFolder() + "\\" + curpageSerial + ".png";

                        //picDraw.Image.Save(backImageFileTmp);
                        picDraw.Image.Save(backImageFile);

                        ELogger.Trace(string.Format("{0}: 图片地址不相等，发生换页. 更新图片. 旧图片地址 {1}, 新图片地址 {2}", penserial, curpageSerial, pageserial));
                        if (graphics != null)
                        {
                            picDraw.Image.Dispose();
                            picDraw.Image = null;
                            graphics.Dispose();
                            graphics = null;
                            ELogger.Trace(string.Format("{0}: 图片地址不相等，释放 graphics ", penserial));
                        }
                        //File.Delete(backImageFile);
                        //File.Move(backImageFileTmp, backImageFile);
                        Image img = Image.FromFile(dicPagesPath[pageserial]);
                        Image bitmap = new Bitmap(img);
                        img.Dispose();
                        graphics = Graphics.FromImage(bitmap);
                        if (dicPagesPath[pageserial] != null && bitmap != null)
                        {
                            ELogger.Trace(string.Format("{0}: 更新图片成功，当前图片地址 {1} ", penserial, pageserial));
                        }
                        else
                        {
                            ELogger.Trace(string.Format("{0}: 更新图片失败，需要的图片地址 {1}", penserial, pageserial));

                        }
                        if (/*dicPages[pageserial]*/bitmap.Width > /*dicPages[pageserial]*/bitmap.Height)
                        {
                            float scal = (float)(2 * A4_WIDTH_DRAW * 0.8 / (2 * A4_WIDTH / 0.3 * 8));
                            graphics.ScaleTransform(scal, scal);
                        }
                        else
                        {
                            float scal = (float)(A4_WIDTH_DRAW / (A4_WIDTH / 0.3 * 8));
                            graphics.ScaleTransform(scal, scal);
                        }
                        graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                        curpageSerial = pageserial;
                        picDraw.Image = bitmap;
                        ELogger.Trace(string.Format("{0}: 图片更新完毕，当前图片地址 {1}", penserial, pageserial));
                        if (bShowPaperBackground && dicPaperBackgrounds.ContainsKey(pageserial))
                        {
                            picDraw.BackgroundImage = dicPaperBackgrounds[pageserial];
                            this.picDraw.BackgroundImageLayout = ImageLayout.Zoom;
                            ELogger.Trace(string.Format("{0}: 将试卷背景图片设置为：{1}", penserial, pageserial));

                        }
                        else
                        {
                            picDraw.BackgroundImage = null;
                            ELogger.Trace(string.Format("{0}: 将试卷背景图片设置为：NULL", penserial));

                        }

                    }
                    catch (Exception ex)
                    {
                        ELogger.Trace("!!!!!!!!!!!!!!!!!!!!02");
                        ELogger.Error(string.Format("Exception 02，笔序号:{0}", penserial));
                        ELogger.Error(string.Format("Exception 02，异常信息:{0}", ex.Message));
                        ELogger.Error(string.Format("Exception 02，栈信息:{0}", ex.StackTrace));
                    }

                }

            }
        }
        /// <summary>
        /// 落笔事件
        /// </summary>

        public void OnPenDown(string penserial)
        {

            if (this.InvokeRequired)
            {
                Action<string> ac = new Action<string>(OnPenDown);
                this.Invoke(ac, new object[] { penserial });
            }
            else
            {
                penDownTime = DateTime.Now;
                labWriteStatus.Text = "书写中";
            }
        }
        /// <summary>
        /// 抬笔事件
        /// </summary>
        /// <param name="penserial"></param>
        public void OnPenUp(string penserial)
        {
            if (this.InvokeRequired)
            {
                Action<string> ac = new Action<string>(OnPenUp);
                this.Invoke(ac, new object[] { penserial });
            }
            else
            {
                labWriteStatus.Text = "未书写";
                if (strokePoints.Count == 0)
                {
                    ELogger.Fatal(string.Format("{0}:坐标点个数为0，可能是在非点阵纸上书写或丢失笔迹", penserial));
                    return;
                }
                try
                {
                    //抬笔后对整个一个笔画进行绘制
                    if (strokePoints.Count == 1)
                        graphics.DrawLine(new Pen(Brushes.Black, 3), strokePoints[0], strokePoints[0]);
                    else if (strokePoints.Count == 2)
                        graphics.DrawLine(new Pen(Brushes.Black, 3), strokePoints[0], strokePoints[1]);
                    else
                        graphics.DrawCurve(new Pen(Brushes.Black, 3), strokePoints.ToArray());

                    picDraw.Invalidate();
                }
                catch (Exception ex)
                {
                    ELogger.Trace("!!!!!!!!!!!Draw error");
                    //MessageBox.Show("Exception 03:" + ex.StackTrace + ex.Message);
                    ELogger.Error(string.Format("绘制线条出错，笔序号:{0}", penserial));
                    ELogger.Error(string.Format("绘制线条出错，异常信息:{0}", ex.Message));
                    ELogger.Error(string.Format("绘制线条出错，栈信息:{0}", ex.StackTrace));
                }
                strokePoints.Clear();
            }


        }
        /// <summary>
        /// 设置窗口显示的文字为学生姓名
        /// </summary>
        /// <param name="name">姓名</param>
        public void SetName(string name)
        {
            if (isSetname)
                return;
            ChangePenserialToName(name);
        }
        /// <summary>
        /// 将窗口显示的笔的序列号更改为学生姓名
        /// </summary>
        /// <param name="name"></param>
        private void ChangePenserialToName(string name)
        {
            if (this.InvokeRequired)
            {
                Action<string> ac = new Action<string>(ChangePenserialToName);
                this.Invoke(ac, new object[] { name });
            }
            else
            {
                isSetname = true;
                labPenID.Text = name;
            }
        }
        public void SetPaperBackgroundImages(Dictionary<string, Image> imgs)
        {
            dicPaperBackgrounds = imgs;
            foreach (var item in dicPaperBackgrounds)
            {
                int picWidth = 0, picHeight = 0;
                if (item.Value.Width > item.Value.Height)
                {
                    picWidth = (int)(2 * A4_WIDTH_DRAW * 0.8);
                    picHeight = (int)(A4_HEIGHT_DRAW * 0.8);
                }
                else
                {
                    picHeight = A4_HEIGHT_DRAW;
                    picWidth = A4_WIDTH_DRAW;
                }

                Bitmap img = new Bitmap(picWidth, picHeight);
                //dicPages.Add(item.Key, img);
                string backImageFile = GetDrawingImageFolder() + "\\" + item.Key + ".png";
                img.Save(backImageFile);
                img.Dispose();
                if (dicPagesPath.ContainsKey(item.Key) == false)
                {
                    dicPagesPath.Add(item.Key, backImageFile);
                    ELogger.Trace(string.Format("创建书写图片：{0}", item.Key));

                }
            }
            return;
            //if (dicPaperBackgrounds != null&&dicPaperBackgrounds.Count==0)
            //    dicPaperBackgrounds = imgs;
            //else if(dicPaperBackgrounds==null)
            //    dicPaperBackgrounds = imgs;
            //else
            //{
            //    dicPaperBackgrounds.Clear();
            //    dicPaperBackgrounds = imgs;
            //}


        }
        private void ControlStatus_Load(object sender, EventArgs e)
        {
            this.picDraw.BackgroundImageLayout = ImageLayout.Zoom;
            //if (Program.ResumeMode)
            //{
            //    ELogger.Trace("数据恢复模式，恢复背景图片");
            //    string backgroundImagePath=GetDrawingImageFolder();
            //    DirectoryInfo di = new DirectoryInfo(backgroundImagePath);
            //    FileInfo[] fi = di.GetFiles();
            //    foreach (var item in fi)
            //    {
            //        string fileName = item.Name.Substring(0, item.Name.LastIndexOf('.'));
            //        if (dicPagesPath.ContainsKey(fileName) == false)
            //        {
            //            dicPagesPath.Add(fileName, item.FullName);
            //            ELogger.Trace(string.Format("数据恢复模式，恢复书写图片：{0}",fileName));
            //        }
            //    }

            //    if (dicPaperBackgrounds.Count == 0)
            //        return;
            //    foreach (var item in dicPaperBackgrounds)
            //    {
            //        if (dicPagesPath.ContainsKey(item.Key))
            //            continue;
            //        int picWidth = 0, picHeight = 0;
            //        if (item.Value.Width > item.Value.Height)
            //        {
            //            picWidth = 2 * A4_WIDTH;
            //            picHeight = A4_HEIGHT;
            //        }
            //        else
            //        {
            //            picHeight = A4_HEIGHT;
            //            picWidth = A4_WIDTH;
            //        }

            //        Bitmap img = new Bitmap(picWidth, picHeight);
            //        //dicPages.Add(item.Key, img);
            //        string backImageFile = GetDrawingImageFolder() + "\\" + item.Key + ".png";
            //        img.Save(backImageFile);
            //        img.Dispose();
            //        if (dicPagesPath.ContainsKey(item.Key) == false)
            //        {
            //            dicPagesPath.Add(item.Key, backImageFile);
            //            ELogger.Trace(string.Format("创建书写图片：{0}", item.Key));

            //        }
            //    }

            //}
            //else
            {
                if (!ResumeMode)
                    DeleteDrawingImageFolder();
                this.picDraw.BackgroundImageLayout = ImageLayout.Zoom;
                if (dicPaperBackgrounds.Count == 0)
                    return;
                foreach (var item in dicPaperBackgrounds)
                {
                    string backImageFile = GetDrawingImageFolder() + "\\" + item.Key + ".png";
                    if (File.Exists(backImageFile))
                    {
                        if (dicPagesPath.ContainsKey(item.Key) == false)
                        {
                            dicPagesPath.Add(item.Key, backImageFile);
                            ELogger.Trace(string.Format("恢复书写图片：{0}", item.Key));

                        }
                        continue;
                    }
                    int picWidth = 0, picHeight = 0;
                    if (item.Value.Width > item.Value.Height)
                    {
                        picWidth = (int)(2 * A4_WIDTH_DRAW * 0.8);
                        picHeight = (int)(A4_HEIGHT_DRAW * 0.8);
                    }
                    else
                    {
                        picHeight = A4_HEIGHT_DRAW;
                        picWidth = A4_WIDTH_DRAW;
                    }

                    Bitmap img = new Bitmap(picWidth, picHeight);
                    //dicPages.Add(item.Key, img);

                    img.Save(backImageFile);
                    img.Dispose();
                    if (dicPagesPath.ContainsKey(item.Key) == false)
                    {
                        dicPagesPath.Add(item.Key, backImageFile);
                        ELogger.Trace(string.Format("创建书写图片：{0}", item.Key));

                    }
                }
                //显示第一张图片
                if (bShowPaperBackground)
                {
                    foreach (var item in dicPaperBackgrounds)
                    {
                        picDraw.BackgroundImage = item.Value;
                        this.picDraw.BackgroundImageLayout = ImageLayout.Zoom;
                        break;
                    }
                }
            }
        }
        /// <summary>
        /// 获取控件显示名称
        /// </summary>
        /// <returns></returns>
        public string GetWindowPenName()
        {
            return labPenID.Text;
        }

        public string GetPenSerial()
        {
            return penSerial;
        }
        /// <summary>
        /// 更改窗体笔的序列号
        /// </summary>
        /// <param name="ps"></param>
        public void ReplacePenSerial(string ps)
        {

            string pathNew = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\WiseClass";
            if (!Directory.Exists(pathNew))
                Directory.CreateDirectory(pathNew);
            pathNew = pathNew + "\\" + ps;
            if (!Directory.Exists(pathNew))
                Directory.CreateDirectory(pathNew);
            if (!string.IsNullOrEmpty(curpageSerial))
            {
                string backImageFileTmp = GetDrawingImageFolder() + "\\" + curpageSerial + "_1.png";
                string backImageFile = GetDrawingImageFolder() + "\\" + curpageSerial + ".png";

                picDraw.Image.Save(backImageFile);
                //picDraw.Image.Save(backImageFileTmp);\
                if (graphics != null)
                {
                    picDraw.Image.Dispose();
                    picDraw.Image = null;
                    graphics.Dispose();
                    graphics = null;
                }
                //File.Delete(backImageFile);
                //File.Move(backImageFileTmp, backImageFile);
            }


            Dictionary<string, string> tmp = new Dictionary<string, string>();
            foreach (var item in dicPagesPath)
            {
                string oldPic = item.Value;
                string newPic = item.Value.Replace(penSerial, ps);
                File.Copy(oldPic, newPic, true);
                tmp.Add(item.Key, newPic);
            }
            dicPagesPath.Clear();
            foreach (var item in tmp)
            {
                dicPagesPath.Add(item.Key, item.Value);
            }
            penSerial = ps;
            if (!string.IsNullOrEmpty(curpageSerial))
            {
                Image img = Image.FromFile(dicPagesPath[curpageSerial]);
                Image bitmap = new Bitmap(img);
                img.Dispose();
                graphics = Graphics.FromImage(bitmap);

                if (/*dicPages[pageserial]*/bitmap.Width > /*dicPages[pageserial]*/bitmap.Height)
                {
                    float scal = (float)(2 * A4_WIDTH_DRAW * 0.8 / (2 * A4_WIDTH / 0.3 * 8));
                    graphics.ScaleTransform(scal, scal);
                }
                else
                {
                    float scal = (float)(A4_WIDTH_DRAW / (A4_WIDTH / 0.3 * 8));
                    graphics.ScaleTransform(scal, scal);
                }
                graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                //curpageSerial = pageserial;
                picDraw.Image = bitmap;
            }

        }
        /// <summary>
        /// 设置是否显示背景图片
        /// </summary>
        /// <param name="visible"></param>
        public void SetBackgroundVisible(bool visible)
        {
            if (this.bShowPaperBackground == visible)
                return;
            if (visible)
            {
                if (!string.IsNullOrEmpty(curpageSerial))
                {
                    if (dicPaperBackgrounds.ContainsKey(curpageSerial))
                    {
                        //picDraw.BackgroundImage.Dispose();
                        //picDraw.BackgroundImage = null;
                        picDraw.BackgroundImage = dicPaperBackgrounds[curpageSerial];
                        this.picDraw.BackgroundImageLayout = ImageLayout.Zoom;
                        ELogger.Trace(string.Format("{0}: 将试卷背景图片设置为：{1}", penSerial, curpageSerial));
                    }

                }
                else
                {
                    if (dicPaperBackgrounds != null && dicPaperBackgrounds.Count > 0)
                    {
                        //picDraw.BackgroundImage.Dispose();
                        //picDraw.BackgroundImage = null;
                        foreach (var item in dicPaperBackgrounds)
                        {
                            this.picDraw.BackgroundImageLayout = ImageLayout.Zoom;
                            picDraw.BackgroundImage = item.Value;

                            ELogger.Trace(string.Format("{0}: 将试卷背景图片设置为：{1}", penSerial, item.Key));
                            break;
                        }
                    }
                }
            }
            else
            {
                //picDraw.BackgroundImage.Dispose();
                //picDraw.BackgroundImage = null;
                Bitmap bt = new Bitmap(100, 100);
                Graphics gr = Graphics.FromImage(bt);
                gr.FillRectangle(new SolidBrush(Color.White), 0, 0, 100, 100);
                gr.Dispose();
                gr = null;
                this.picDraw.BackgroundImageLayout = ImageLayout.Stretch;

                picDraw.BackgroundImage = bt;
            }
            bShowPaperBackground = visible;
        }

        private void labPenID_Resize(object sender, EventArgs e)
        {
            //labPenID.Visible = false;
        }

        private void DrawingWindow_SizeChanged(object sender, EventArgs e)
        {
            //labPenID.Visible = true;
        }
        public void SetLabelVisable(bool visable)
        {
            //labPenID.Visible = visable;
            pnlStatus.Visible = visable;
        }

        private void picDraw_MouseDown(object sender, MouseEventArgs e)
        {
            this.picDraw.DoDragDrop(this, DragDropEffects.Copy);
        }
    }
    class StrokeItem
    {
        public List<Point> stroke;
        public DateTime pointTime;
        public string pageSerial;
        public string PenSerial;
        
    }

}
